package com.lxs.mall.controller;

import com.github.pagehelper.PageInfo;
import com.lxs.mall.common.ApiRestResponse;
import com.lxs.mall.common.Constant;
import com.lxs.mall.exception.LxsMallExceptionEnum;
import com.lxs.mall.model.pojo.Category;
import com.lxs.mall.model.pojo.User;
import com.lxs.mall.model.request.AddCategoryReq;
import com.lxs.mall.model.request.UpdateCategoryReq;
import com.lxs.mall.model.vo.CategoryVo;
import com.lxs.mall.service.CategoryService;
import com.lxs.mall.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 小松
 */
@Controller
public class CategoryController {
    @Resource
    UserService userService;
    @Resource
    CategoryService categoryService;

    /**
     * 后台添加目录
     */
    @ApiOperation("后台添加目录")
    @PostMapping("/admin/category/add")
    @ResponseBody
    public ApiRestResponse addCategory(HttpSession httpSession,
                                      @Valid @RequestBody AddCategoryReq addCategoryReq) {
        /*使用@Valid注解进行参数限制，在AddCategoryReq类中使用@max,@size,@notnull*/
        //if (addCategoryReq.getName() == null || addCategoryReq.getOrderNum() == null ||
        //        addCategoryReq.getParentId() == null || addCategoryReq.getType() == null) {
        //    return ApiRestResponse.error(LxsMallExceptionEnum.PARA_NO_NULL);
        //}
        User currentUser = (User) httpSession.getAttribute(Constant.LXS_MALL_USER);
        if (currentUser == null) {
            return ApiRestResponse.error(LxsMallExceptionEnum.NEED_LOGIN);
        }
        //检验管理员
        boolean adminRole = userService.checkAdmin(currentUser);
        if (adminRole) {
            //是管理员
            categoryService.add(addCategoryReq);
            return ApiRestResponse.success();
        } else {
            //不是
            return ApiRestResponse.error(LxsMallExceptionEnum.NEED_LOGIN);
        }
    }
    @ApiOperation("后台更新目录")
    @PostMapping("/admin/category/update")
    @ResponseBody
    public ApiRestResponse updateCategory(HttpSession httpSession,@Valid @RequestBody UpdateCategoryReq updateCategoryReq){
        User currentUser = (User) httpSession.getAttribute(Constant.LXS_MALL_USER);
        if (currentUser == null) {
            return ApiRestResponse.error(LxsMallExceptionEnum.NEED_LOGIN);
        }
        //检验管理员
        boolean adminRole = userService.checkAdmin(currentUser);
        if (adminRole) {
            //是管理员
            Category category = new Category();
            BeanUtils.copyProperties(updateCategoryReq,category);
            categoryService.update(category);
            return ApiRestResponse.success();
        } else {
            //不是
            return ApiRestResponse.error(LxsMallExceptionEnum.NEED_LOGIN);
        }
    }
    @ApiOperation("后台删除目录")
    @PostMapping("/admin/category/delete")
    @ResponseBody
    public ApiRestResponse deleteCategory(@RequestParam Integer id){
        categoryService.delete(id);
        return ApiRestResponse.success();
    }
    @ApiOperation("后台目录列表")
    @GetMapping("/admin/category/list")
    @ResponseBody
    public ApiRestResponse listCategoryForAdmin(@RequestParam Integer pageNum,
                                                @RequestParam Integer pageSize){
        PageInfo pageInfo = categoryService.listForAdmin(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }
    @ApiOperation("前台目录列表")
    @GetMapping("/category/list")
    @ResponseBody
    public ApiRestResponse listCategoryForCustomer(){
        List<CategoryVo> categoryVos = categoryService.listCategoryForCustomer(0);
        return ApiRestResponse.success(categoryVos);
    }
}
